package imaa.escola.turma.service;

import imaa.escola.aluno.domain.AlunoTurma;
import imaa.escola.aluno.repository.AlunoRepository;
import imaa.escola.disciplina.repository.DisciplinaRepository;
import imaa.escola.professor.repository.ProfessorRepository;
import imaa.escola.turma.datatransfer.MatriculaDto;
import imaa.escola.turma.datatransfer.PesquisaTurmaDto;
import imaa.escola.turma.datatransfer.TurmaDto;
import imaa.escola.turma.domain.Turma;
import imaa.escola.turma.repository.MatriculaRepository;
import imaa.escola.turma.repository.TurmaRepository;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/turma")
@Controller
public class TurmaService {

	private static final Logger logger = Logger.getLogger(TurmaService.class);

	@Autowired
	TurmaRepository turmaRepository;

	@Autowired
	DisciplinaRepository disciplinaRepository;
	
	@Autowired
	ProfessorRepository professorRepository;
	
	@Autowired
	AlunoRepository alunoRepository;
	
	@Autowired
	MatriculaRepository matriculaRepository;

	@Transactional
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public TurmaDto salvar(@RequestBody TurmaDto dto) {

		Turma turma = dto.convertToEntity();

		turma.setDisciplina(disciplinaRepository.findOne(dto.disciplina.id));
		turma.setProfessor(professorRepository.findOne(dto.professor.id));

		TurmaDto result = new TurmaDto(turmaRepository.save(turma));

		logger.info("Turma inserida na base de dados. ID: " + result.id);

		return result;
	}
	
	@Transactional
	@ResponseBody
	@RequestMapping(value = "/matricula", method = RequestMethod.POST)
	public MatriculaDto matricular(@RequestBody MatriculaDto dto) {

		AlunoTurma matricula = dto.convertToEntity();

		matricula.setAluno(alunoRepository.findOne(dto.aluno.id));
		matricula.setTurma(turmaRepository.findOne(dto.turma.id));

		MatriculaDto result = new MatriculaDto(matriculaRepository.save(matricula));

		logger.info("Matricula inserida na base de dados. ID: " + result.id);

		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/get/{idTurma}", method = RequestMethod.GET, produces = "application/json")
	public TurmaDto getTurma(@PathVariable Long idTurma) {
		return new TurmaDto(turmaRepository.findOne(idTurma));
	}

	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public List<TurmaDto> listarTurma() {
		List<Turma> lista = turmaRepository.findAll();
		List<TurmaDto> listaDto = new ArrayList<TurmaDto>();
		for (Turma turma : lista) {
			TurmaDto dto = new TurmaDto(turma);
			listaDto.add(dto);
		}
		return listaDto;
	}

	@ResponseBody
	@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json")
	public List<TurmaDto> pesquisarTurma(@RequestParam(value = "descricao") String dto) {
		List<Turma> lista = turmaRepository.findByName(dto);
		List<TurmaDto> listaDto = new ArrayList<TurmaDto>();
		for (Turma turma : lista) {
			TurmaDto turmaDto = new TurmaDto(turma);
			listaDto.add(turmaDto);
		}
		return listaDto;
	}

	@RequestMapping(value = "/advancedSearch", method = RequestMethod.POST)
	public @ResponseBody
	List<TurmaDto> pesquisaAvancada(@RequestBody PesquisaTurmaDto dto) {
		List<TurmaDto> lista = turmaRepository.findByExample(dto);
		return lista;
	}
		
	@ResponseBody
	@RequestMapping(value = "/remove/{idTurma}", method = RequestMethod.GET, produces = "application/json")
	public void removeTurma(@PathVariable Long idTurma) {
		turmaRepository.delete(idTurma);
	}
}
